package com.lwj.controller;

import com.lwj.model.quartz.JobInfo;
import com.lwj.service.quartz.QuartzService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * created by lwj 2020/9/14 17:52
 **/
@Api(tags = "定时任务")
@RestController
@RequestMapping("/api/quartz")
public class QuartzController {

    @Autowired
    private QuartzService quartzService;

    @PostMapping("/save")
    public void addJob(@RequestBody JobInfo jobInfo) {
        quartzService.addJobByTime(jobInfo);
    }

    @DeleteMapping(value = "/delete")
    public void deleteTask(@RequestParam("jobName") String jobName,@RequestParam("jobGroup") String jobGroup) {
        quartzService.deleteJob(jobName, jobGroup);
    }

    @GetMapping("/jobs")
    public List<JobInfo> getTasks() {
        return quartzService.queryAllJob();
    }

    @GetMapping("/jobs/isRunning")
    public List<JobInfo> getRunningTasks() {
        return quartzService.queryRunJob();
    }

    @PutMapping("/{jobName}/{jobGroup}/{command}")
    public void pause(@PathVariable("jobName") String jobName, @PathVariable("jobGroup") String jobGroup, @PathVariable("command") String command) {
        if ("pause".equals(command)) {
            quartzService.pauseJob(jobName, jobGroup);
        } else if ("resume".equals(command)) {
            quartzService.resumeJob(jobName, jobGroup);
        }
    }
    @GetMapping("/run")
    public void runAJobNow(@RequestParam("jobName")String jobName,@RequestParam("jobGroup")String jobGroup) {
        quartzService.runAJobNow(jobName, jobGroup);
    }

    @GetMapping("/update")
    public void updateJob(@RequestParam("jobName")String jobName, @RequestParam("jobGroup")String jobGroup, @RequestParam("jobName")String jobTime) {
        quartzService.updateJob(jobName, jobGroup, jobTime);
    }
}
